package com.pap.msg.message.controller;

import com.pap.base.util.bean.BeanCopyUtilss;
import com.pap.beans.idworker.IdWorker;
import com.pap.msg.dto.MsgContentDTO;
import com.pap.msg.message.entity.MsgContent;
import com.pap.msg.message.service.IMsgContentService;
import com.pap.obj.vo.logineduser.LoginedUser;
import com.pap.obj.vo.logineduser.LoginedUserVO;
import com.pap.obj.vo.page.Page;
import com.pap.obj.vo.response.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/msgcontent")
@Api(value = "消息内容信息", tags = "消息内容信息", description="消息内容信息")
public class MsgContentController {

	private static Logger logger  =  LoggerFactory.getLogger(MsgContentController.class);
	
	@Resource(name = "msgContentService")
	private IMsgContentService msgContentService;

	@Resource(name = "idWorker")
	private IdWorker idWorker;

	@ApiOperation("创建")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "papToken", value = "登录标识", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "papVersion", value = "版本号", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "msgContentDTO", value = "", required = true, dataType = "MsgContentDTO", paramType="body")
	})
	@PostMapping(value = "")
	public ResponseVO<MsgContentDTO> create(@RequestHeader(value = "papToken", defaultValue = "-1") String papToken,
											@RequestHeader(value = "papVersion", defaultValue = "1.0") String papVersion,
											@RequestBody MsgContentDTO msgContentDTO,
											@LoginedUser @ApiIgnore LoginedUserVO loginedUserVO){
		String idStr = idWorker.nextIdStr();
		msgContentDTO.setMsgContentId(idStr);

		MsgContent databaseObj = new MsgContent();
		BeanCopyUtilss.myCopyProperties(msgContentDTO, databaseObj, loginedUserVO, true);

		int i = msgContentService.insertSelective(databaseObj);

		return ResponseVO.successdata(msgContentDTO);
	}

	@ApiOperation("更新")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "papToken", value = "登录标识", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "papVersion", value = "版本号", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "msgContentDTO", value = "", required = true, dataType = "MsgContentDTO", paramType="body")
	})
	@PutMapping(value = "")
	public ResponseVO<MsgContentDTO> update(@RequestHeader(value = "papToken", defaultValue = "-1") String papToken,
													@RequestHeader(value = "papVersion", defaultValue = "1.0") String papVersion,
													@RequestBody MsgContentDTO msgContentDTO,
											@LoginedUser @ApiIgnore LoginedUserVO loginedUserVO){

		MsgContent databaseObj = new MsgContent();
		BeanCopyUtilss.myCopyProperties(msgContentDTO, databaseObj, loginedUserVO, false);
		int i = msgContentService.updateByPrimaryKeySelective(databaseObj);

		databaseObj = msgContentService.selectByPrimaryKey(databaseObj.getMsgContentId());

		return ResponseVO.successdata(databaseObj);
	}


	@ApiOperation("删除")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "papToken", value = "登录标识", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "papVersion", value = "版本号", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "msgContentDTO", value = "", required = true, dataType = "MsgContentDTO", paramType="body")
	})
	@DeleteMapping(value = "")
	public ResponseVO<String> delete(@RequestHeader(value = "papToken", defaultValue = "-1") String papToken,
													@RequestHeader(value = "papVersion", defaultValue = "1.0") String papVersion,
													@RequestBody MsgContentDTO msgContentDTO){

		int i = msgContentService.deleteByPrimaryKey(msgContentDTO.getMsgContentId());

		return ResponseVO.successdata("操作成功");
	}

	@ApiOperation("详情")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "papToken", value = "登录标识", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "papVersion", value = "版本号", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "id", value = "编号", required = true, dataType = "String", paramType="path")
	})
	@GetMapping(value = "/{id}")
	public ResponseVO<MsgContentDTO> selectOne(@RequestHeader(value = "papToken", defaultValue = "-1") String papToken,
													@RequestHeader(value = "papVersion", defaultValue = "1.0") String papVersion,
													@PathVariable("id") String id){

		MsgContent databaseObj = msgContentService.selectByPrimaryKey(id);

		return ResponseVO.successdata(databaseObj);
	}

	@ApiOperation("获取指定消息类型的最新消息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "papToken", value = "登录标识", required = false, paramType = "header", dataType = "String"),
			@ApiImplicitParam(name = "papVersion", value = "版本号", required = false, paramType = "header", dataType = "String"),
			@ApiImplicitParam(name = "msgContentType", value = "消息类型", required = true, dataType = "String", paramType="path")
	})
	@GetMapping(value = "selectTop1ByMsgContentType/{msgContentType}")
	public ResponseVO<MsgContentDTO> selectTop1ByMsgContentType(@RequestHeader(value = "papToken", defaultValue = "-1") String papToken,
											   @RequestHeader(value = "papVersion", defaultValue = "1.0") String papVersion,
											   @PathVariable("msgContentType") String msgContentType){
		Map<Object, Object> map = new HashMap<>();
		map.put("msgContentType", msgContentType);
		List<MsgContent> msgContentList = msgContentService.selectListByMap(map);
		if(msgContentList != null && msgContentList.size() > 0) {
			return ResponseVO.successdata(msgContentList.get(0));
		}
		return ResponseVO.successdata(new MsgContentDTO());
	}

	@ApiOperation("查询")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "papToken", value = "登录标识", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "papVersion", value = "版本号", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "msgContentDTO", value = "应用查询参数", required = false, dataType = "MsgContentDTO", paramType="body"),
		@ApiImplicitParam(name = "pageNo", value = "页码", required = true, dataType = "int", paramType="query"),
		@ApiImplicitParam(name = "pageSize", value = "每页记录数", required = true, dataType = "int", paramType="query"),
		@ApiImplicitParam(name = "sortBy", value = "按照哪个属性排序", required = false, dataType = "String", paramType="query"),
		@ApiImplicitParam(name = "order", value = "排序顺序", required = false, dataType = "String", paramType="query")
	})
	@PostMapping(value = "/query")
	public ResponseVO<MsgContentDTO>query(@RequestHeader(value = "papToken", defaultValue = "-1") String papToken,
												@RequestHeader(value = "papVersion", defaultValue = "1.0") String papVersion,
												@RequestBody MsgContentDTO msgContentDTO,
												@RequestParam Integer pageNo, @RequestParam Integer pageSize,
												@RequestParam(defaultValue = "create_time") String sortBy, @RequestParam(defaultValue = "desc") String order,
										  @LoginedUser @ApiIgnore LoginedUserVO loginedUserVO){
		Map<Object, Object> map = BeanCopyUtilss.beanToMap(msgContentDTO, loginedUserVO);
		if(pageSize != 0) {
			Page newPage = new Page(pageNo + "", pageSize + "");
			map.put("page", newPage);
		}
		List<MsgContent> list = msgContentService.selectListByMap(map);

		//
		List<MsgContentDTO> dtoList = toDTO(list);

		if(pageSize != 0) {
			Page newPage = new Page(pageNo + "", pageSize + "");
			int countNum = msgContentService.selectCountByMap(map);
			newPage.setCount(countNum);
			return ResponseVO.successdatas(dtoList, newPage);
		} else {
			return ResponseVO.successdatas(dtoList, null);
		}

	}

	private List<MsgContentDTO> toDTO(List<MsgContent> databaseList) {
		List<MsgContentDTO> returnList = new ArrayList<MsgContentDTO>();
		if(databaseList != null) {
			for(MsgContent dbEntity : databaseList) {
				MsgContentDTO dtoTemp = new MsgContentDTO();
				BeanCopyUtilss.myCopyProperties(dbEntity, dtoTemp);
				returnList.add(dtoTemp);
			}
		}
		return returnList;
	}

	private List<MsgContent> toDB(List<MsgContentDTO> dtoList) {
		List<MsgContent> returnList = new ArrayList<MsgContent>();
		if(dtoList != null) {
			for(MsgContentDTO dtoEntity : dtoList) {
				MsgContent dbTemp = new MsgContent();
				BeanCopyUtilss.myCopyProperties(dtoEntity, dbTemp);
				returnList.add(dbTemp);
			}
		}
		return returnList;
	}
}
